#ifndef RESOURCE_asocket_H
#define RESOURCE_asocket_H
#include <queue>
#include "async/aio.h"
#include "async/function.h"
#include "socket.h"


class event_loop;


class asocket : public async_io {
public:
    friend class event_loop;
    asocket(int family, int type, int protocol);
    explicit asocket(class socket&&);
    virtual ~asocket();

    inline int fileno()const { return sock.fileno(); }

protected:
    //!  event
    virtual void on_error();
    virtual void on_write();
    virtual void on_close();
    virtual void on_read();

    //! interface
    virtual void closing() {/** user can override it when socket is closing **/};

public:
    //! interface
    void open(class socket&& sock, event_loop *loop=nullptr);    //  open socket connection, closing state

    inline const socket::address& local()const noexcept { return sock.local; }
    inline const socket::address& remote()const noexcept { return sock.remote; }

protected:
    int event_mask = 0;
    class socket sock;
    async_function coro;
    event_loop *loop = nullptr;
    bool _closing = false;
};


#endif //RESOURCE_asocket_H
